const app=getApp();
function getData(data,that){
    var type=data.type;     //页面参数
    wx.showLoading({
        title: '数据加载中...',
        mask:true,
    })
    wx.request({
        url: app.apiHost + 'article/index',
        data: data,
        method: 'GET',
        dataType: 'JSON',
        success: function (res) {
            console.log(res);
            var data = JSON.parse(res.data);
            var obj = { articleList: data.content.articleList};
            if (data.content.bannerList){
                obj.bannerList = data.content.bannerList;
            }
            if (type>0){
                obj.type = type;
            }           
            that.setData(obj);
        },
        complete:function(){
            setTimeout(()=>{
                wx.hideLoading();
                wx.stopPullDownRefresh();
            },500);
            
        }
    })
}
// 统一请求方法
function request(url,data,msg='',fuc='',that='',successmsg=''){     //url:接口地址,data:接口参数 ,msg:提示信息,fuc:执行函数,that:page的引用,用来更新视图
    if(msg){
        wx.showLoading({
            title: msg,
        })
    }
    wx.request({
        url: url,
        method:'GET',
        data:data,
        success:function(res){
            var data=res.data;
            var code=res.data.code;
            if(code==200){
                if(successmsg){
                    toast(successmsg);
                }
                if(typeof(fuc)=='function'){
                    fuc(data.content);  //这是收到接口返回成功后的回调函数,参数是接口返回的数据
                }
            }else if(code==3){     //code=3是未登录
                app.toLogin();
            }else{
                toast(data.msg);
            }
            
        },
        complete:function(){
            if(msg){
                setTimeout(()=>wx.hideLoading(),500);
            }
            
        }
        
    })
}
// 获取用户ID
function getUserId(){
    
    var userInfo = wx.getStorageSync("userInfo");
    
    if (userInfo.userId) {
        
        return userInfo.userId;
    }
}
// 获取用户token
function getToken(){
    
    var userInfo = wx.getStorageSync("userInfo");
    if (userInfo.token) {
        return userInfo.token;
    }
}
// 显示提示信息
function toast(msg){
    setTimeout(() => {
        wx.showToast({
            title: msg,
            icon: 'none'
        })
    }, 600)
}
// 检测是否登录,如果没登录则跳转至登录页面
function checkLogin(redirect=true){
    var userInfo=wx.getStorageSync("userInfo");
    if(!userInfo.token||!userInfo.userId){
        if (redirect){      //是否跳转参数
            wx.navigateTo({
                url: '/pages/login/index',
            });
        }        
        return false;
    }else{
        return true;
    }
}
//注销
function logout(){
    wx.removeStorageSync('userInfo');
    wx.reLaunch({
               
        url: '/pages/index/index',
    })
}
//字符串去掉空格
function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
module.exports = {
    getData: getData,
    request:request,
    getToken:getToken,
    getUserId:getUserId,
    checkLogin:checkLogin,
    toast:toast,
    logout:logout,
    trim:trim,
}